package com.xie.web;

import com.xie.mapper.UserMapper;
import com.xie.service.BaiduWapService;
import com.xie.service.UserService;
import com.xie.utils.LayUiDataUtil;
import org.apache.http.HttpEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@RestController
@RequestMapping("user")
public class User {

    @Autowired
    BaiduWapService baiduWapService;

    @Autowired
    UserMapper userMapper;

    @RequestMapping("index")
    public ModelAndView toIndex(ModelAndView mv){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        com.xie.pojo.User user = userMapper.findByUserName(userDetails.getUsername());
        mv.addObject("userHaveMoney",user.getMoney());
        mv.addObject("userHaveWap",baiduWapService.countByUser());
        mv.setViewName("user-index");
        return mv;
    }

    @RequestMapping("wapData")
    public Map<String,Object> wapData(@RequestParam(name = "page",defaultValue = "1") Integer page,
                                      @RequestParam(name = "limit",defaultValue = "10") Integer limit){
        return new LayUiDataUtil(baiduWapService.findByUser(page,limit),baiduWapService.countByUser()).getData();
    }
}
